<template>
	<om-addpage :menuName="menuName" :busCode="busCode" @formSubmit='formSubmit'>
		<view v-show="false">
			<input v-model="formdata.leaveReasonId" name="leaveReasonId" />
			<input v-model="formdata.leaveReasonName" name="leaveReasonName" />
			<input v-model="formdata.infonameId" name="infonameId" />
		</view>
		<uni-list title="离职信息">
			<uni-list-item :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-5"><om-label label="离职人"></om-label></view>
					<view class="tui-right tui-col-7">{{ userName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-5"><om-label label="申请离职日期" required></om-label></view>
					<view class="tui-right  tui-col-7">
						<picker mode="date" @change="bindDateChange" v-model="formdata.leaveDate" name="leaveDate" :value="formdata.leaveDate">
							<view>{{formdata.leaveDate.trim()?formdata.leaveDate:"请选择"}}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item  @click='showLeaveReason' :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-5"><om-label label="离职事由" required></om-label></view>
					<view class="tui-right tui-col-7">
						{{formdata.leaveReasonName || '请选择'}}
						<om-select ref="popup" title="离职事由" :range="leaveReasons" multiple rangeKey="dicname" @confirm="leaveReasonConfirm"></om-select>
					</view>
				</view>
				<!-- <om-checkgroup 
					:list="leaveReasons" 
					checkedField="leaveReasonIds" 
					nameField="dicname"
					formNameField="leaveReasonName"
					formValueField="leaveReasonId"
					>
				</om-checkgroup> -->
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-5"><om-label label="事由描述" required></om-label></view>
					<view class="tui-left tui-col-7"><textarea :auto-height="true" class="om-textarea" v-model="formdata.reasonInfo" name="reasonInfo" placeholder="请输入..." /></view>
				</view>
			</uni-list-item>
		</uni-list>
	</om-addpage>
</template>

<script>
	const form = require("../../../static/js/formValidation.js")
	import dateTimePicker from '@/static/js/dateTimePicker.js';
	import { ignoreHtml} from '@/static/js/common.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			form: this,
			formPage:this
		}
	},
	data() {
		return {
			formdata: {
				leaveReasonId:'',
				leaveReasonName:'',
				leaveDate: dateTimePicker.getNewDate(),
				infonameId: '',
				reasonInfo: ''
			},
			leaveReasonNames: [],
			leaveReasons: [],
			menuName: '',
			id: '',
			busCode: ''
		};
	},
	computed: mapState(useLoginUserStore, ['userName', 'userId']),
	methods: {
		
		showLeaveReason(){
			this.$refs.popup.change()
		},
		leaveReasonConfirm(e) {
			// this.selectUserList = e;
			// this.formdata.userIds = e.map(e=>e.id).join(',')
			this.formdata.leaveReasonName = e.title
			this.formdata.leaveReasonId = e.value.join(',')
		},
		/**
		 * 补签日期
		 */
		bindDateChange(e) {
			this.formdata.leaveDate = e.detail.value;
		},
		formSubmit: function(formdata) {
			//表单规则
			let rules = [{
				name: "leaveDate",
				rule: ["required"], 
				msg: ["请选择离职日期"]
			}, {
				name: "leaveReasonId",
				rule: ["required"],
				msg: ["请选择离职事由"]
			}, {
				name: "reasonInfo",
				rule: ["required"],
				msg: ["请输入事由描述"]
			}];
			let _that = this;
			if(!formdata || Object.keys(formdata).length==0){
				formdata = _that.formdata
			}
			if(!formdata.infonameId){
				_that.om.request('在线状态异常，请重新登录');
				return
			}
			let checkRes = form.validation(formdata, rules);
			if (!checkRes) {
				_that.om.formSave('wxLeaveApply/save', formdata, _that)
			} else {
				_that.om.toast(checkRes);
			}
			
		},
		/**
		 * 加载离职事由
		 */
		loadLeaveReasons() {
			let _that = this;
			_that.om
				.request('wxLeaveApply/getReasonList', {
					userId: _that.userId
				})
				.then(res => {
					if (res.succeed) {
						_that.leaveReasons = res.value;
					}
				});
		}
	},
	onLoad: function(options) {
		let _that = this;
		_that.menuName = options.title || '离职申请'
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		_that.busCode = options.code || 'HRS0701';
		_that.formdata.infonameId = _that.userId;
		//验证人员是否有试用期岗位
		_that.om
			.request('wxLeaveApply/verifySame', {
				userId: _that.userId
			})
			.then(res => {
				if (res.value) {
					_that.om.toast(ignoreHtml(res.value));
				}
			});
		_that.loadLeaveReasons();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.saveFlag = false;
	}
};
</script>

<style></style>
